package com.mycode.command15.resolve2;

import java.util.Deque;
import java.util.LinkedList;
import java.util.Objects;
import java.util.Queue;

public class AddCommand extends AbstractCommand{

    private Adder adder = new Adder();
    private int value;

    private Deque<Integer> stack = new LinkedList<>();

    // 抽象命令执行方法
    @Override
    public int execute(int value) {
        this.value = value;
        stack.push(value);
        return adder.add(value);
    }

    // 实现抽象命令类 undo()
    @Override
    public int undo() {
        Integer pop = stack.pop();
        if (Objects.isNull(pop)) {
            return 0;
        }
        return adder.add(-pop);
    }


}
